from enum import Enum

import pygame


# 玩家状态
class PlayerState(Enum):
    WAIT = "wait"
    ROLL_DICE = "roll"
    MOVE = "move"
    WIN = "win"
    LOSE = "lose"


class Player(pygame.sprite.Sprite):
    def __init__(self, name, pos, color, bgColor):
        super().__init__()
        self.message = ""
        self.state: PlayerState = PlayerState.WAIT
        self.name: str = name
        self.pos = pos  # 在棋盘的初始位置
        self.color = color
        self.bgColor = bgColor
        self.surface = pygame.Surface((364, 100))
        self.rect = self.surface.get_rect()
        self.font = pygame.font.SysFont("KaiTi", 48)

    def set_message(self, message):
        self.message = message

    def update(self):
        msg = self.font.render(self.message, True, self.color)
        rect = msg.get_rect()
        rect.center = (182, 50)
        self.surface.fill(self.bgColor)
        self.surface.blit(msg, rect)

    def do_roll_dice(self):
        self.state = PlayerState.ROLL_DICE
        self.set_message(self.name + " 掷骰子")
        print(self.name, self.state.value)

    def do_move(self):
        self.state = PlayerState.MOVE
        self.set_message(self.name + " 走棋")
        print(self.name, self.state.value)

    def move_end(self):
        self.state = PlayerState.WAIT
        self.set_message("")
        print(self.name, self.state.value)


def main():
    # 初始化pygame库
    pygame.init()
    # 标题
    pygame.display.set_caption("Player 测试")

    # 创建游戏窗口
    screen = pygame.display.set_mode((400, 300))
    screen_rect = screen.get_rect()

    # screen.fill((204, 204, 204))

    player = Player("测试玩家", (255, 0, 0), (255, 255, 255))
    player.rect.center = screen_rect.center
    player.do_roll_dice()

    # 游戏主循环
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        # screen.fill((204, 204, 204))
        player.update()
        screen.blit(player.surface, player.rect)
        pygame.display.update()


if __name__ == "__main__":
    main()
